﻿/*
Add by Quang 
10/02/2010
Load cac event cho trang Dang ky, Dang nhap moi
*/
function loadboxEvents() {
    var url = document.location.pathname.toLowerCase();
    if (url.indexOf('dangkypartner.aspx') != -1) {
        loadDDLs(); //load DDL cho trang Dang ky partner
    }
    else if (url.indexOf('bosungthongtin.aspx') != -1) {
        loadDDLs();
    }
    else if (url.indexOf('dangky.aspx') != -1) { }
    //edit 03/04/2010
    else if (url.indexOf('dangnhap.aspx') != -1 || url.indexOf('index.aspx') != -1) {
        loadCheckboxRemember($('chk_remember'));
    }

    //cac textbox
    loadTextboxEvent($(document.body));

    //cac checkbox
    //edit 03/04/2010
    if (url.indexOf('dangnhap.aspx') == -1 && url.indexOf('index.aspx') == -1) {
        loadCheckBox(); //checkbox kieu fai chon 1 trong 2
    }

};

/*
Add by Quang 
10/02/2010
Load cac event cho checkbox
*/
function loadCheckboxRemember(checkID) {
    if (checkID) {
        var arrLabel;
        try {
            arrLabel = checkID.up().down('label');
        } catch (ee) { arrLabel = $('lb_remember') }
        arrLabel.observe('click', function(e) {
            var ele = Event.element(e).up('div');
            if (ele.hasClassName('checked')) ele.removeClassName('checked');
            else ele.addClassName('checked');
        });
    }
};

/*DROP DOWN LIST*/
/*
Add by Quang 
10/02/2010
Thay the cac element select mac dinh bang Select kieu moi
*/
function loadDDLs() {
    var ddlDate = new ddlDD();
    ddlDate.initializeCtrl('ddlDate', onLoadDDL, onClickDDL);
    var ddlMonth = new ddlDD();
    ddlMonth.initializeCtrl('ddlMonth', onLoadDDL, onClickDDL);
    var ddlYear = new ddlDD();
    ddlYear.initializeCtrl('ddlYear', onLoadDDL, onClickDDL);
    var ddlPopupEmail = new ddlDD();
    ddlPopupEmail.initializeCtrl('ddlPopupEmail', onLoadDDL, onClickDDL);
    var ddlPopupEmail_ddl = new ddlDD();
    ddlPopupEmail_ddl.initializeCtrl('ddlPopupEmail_ddl', onLoadDDL, onClickDDL);
    
};

/*
Add by Quang 
10/02/2010
Ham duoc goi khi tao Select kieu moi
*/
function onLoadDDL(obj) {
    if (obj.scroller == null) {
        var id = obj.targetID;
        //obj.scroller = new Scrolling.Scroller($(id + '_ddlScroller'), 400, 165);
        //obj.scrollbar = new Scrolling.Scrollbar($(id + '_ddlScrollbar'), obj.scroller, new Scrolling.ScrollTween());
    }
};

/*
Add by Quang 
10/02/2010
Event click tren Dropdownlist
*/
function onClickDDL(id, value) {
    var selectedValue = value;
    //hoac lay gia tri tu dropdown cung dc
    //alert($(id).value)
};

/*
Add by Quang 
10/02/2010
Object Dropdownlist
*/
var ddlDD = Class.create({
    initialize: function() {
        this.targetID = null;
        this.DDL = null;
        this.selectedValue = '';
        this.onClickHandle = null;
        this.onLoadHandle = null;
        this.scroller = null;
        this.scrollbar = null;
    },
    /*
    Add by Quang 
    10/02/2010
    Ham khoi tao
    */
    initializeCtrl: function(targetID, onLoadHandle, onClickHandle, isSmall) {
        var select = $(targetID);
        if (select) {
            this.targetID = targetID;
            this.onClickHandle = onClickHandle;
            this.onLoadHandle = onLoadHandle;
            this.loadHtml(select, isSmall);
        }
    },
    /*
    Add by Quang 
    10/02/2010
    Ham thay the noi dung html
    */
    loadHtml: function(objSelect, isSmall) {        
        var id = this.targetID;
        var objSelected = objSelect.options[objSelect.selectedIndex];
        this.selectedValue = objSelected.value;

        var html = '<div class="bg_select_l"><div class="bg_select_r"><div class="select_inner">' +
            "<div class='dropdownlist'>" +
            "<div class='text'>" +
		        '<span id="' + id + '_SV" class="no_select" UNSELECTABLE="on">' + objSelected.text + '</span>' + "<em></em>" +
	        '</div>' +
	        '<div class="select_list">' +
		    "<div class='toggle' style=''>" +
		    '<ul id="' + id + '_ddlScroller" class="select_options" style="#zoom:1">';
        for (var i = 0, len = objSelect.options.length; i < len; i++) {
            var option = objSelect.options[i];
            html += '<li style="line-height: 40px;font-size: 26px;clear:left; float:left; width:278px; cursor:pointer; padding-left:4px;" class="spanSelect" id="' + id + '_' + option.value + '">' + option.text + '</li>';
        }
        html += '</ul></div>' +
	        '</div></div></div></div>';


        //Quang 23/02        
        this.DDL = new Element('div', { 'id': id + '_ddl', 'class': 'select' + (!isSmall ? '' : ' select_small'), 'style': 'width:' + (objSelect.getWidth() - 36) + 'px;float:left;position:relative;' }).update(html);
        objSelect.insert({ 'before': this.DDL }).hide();
        if (this.onLoadHandle && typeof (this.onLoadHandle) == 'function') {
            this.onLoadHandle(this);
        }
        this.DDL.down('.select_list').hide();
        this.loadEvents();
    },
    /*
    Add by Quang 
    10/02/2010
    Gan events cho dropdownlist
    */
    loadEvents: function() {
        this.DDL.observe('click', this.DDLClick.bindAsEventListener(this));
        document.observe('click', this.docClick);
    },
    /*
    Add by Quang 
    10/02/2010
    Event click tren ddl
    */
    DDLClick: function(e) {
        var ele = Event.element(e);
        while (ele.id == '') ele = ele.up();
        if (ele.id == this.targetID + '_SV' || ele.id == this.targetID + '_ddl') {
            var ddlList = this.DDL.down('.select_list');
            if (ddlList.visible()) ddlList.hide();
            else {
                this.docClick(e);
                ddlList.show();
            }
        }
        else if (ele.tagName == 'LI') {
            var newValue = ele.id.replace(this.targetID + '_', '');
            if (this.selectedValue != newValue) {
                this.selectedValue = newValue;
                $(this.targetID).value = this.selectedValue;
                if (this.onClickHandle && typeof (this.onClickHandle) == 'function') {
                    this.onClickHandle(this.targetID, this.selectedValue);
                }
                this.DDL.down('#' + this.targetID + '_SV').update(ele.innerHTML);
            }
            this.DDL.down('.select_list').hide();
        }
        Event.stop(e);
    },
    /*
    Add by Quang 
    10/02/2010
    Event click tren 1 element khac ddl, hide ddl di
    */
    docClick: function(e) {
        $(document.body).select('.select_list').invoke('hide');
    }
});
/*END DROP DOWN LIST*/


